package com.fang.gateway.filter;

import com.fang.common.common.CommonConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description
 * @Author Bernie_fang
 * @Date 2025/5/27 22:37
 **/
@Slf4j
@Configuration
public class LogTraceFilterConfig {
    @Bean
    public GlobalFilter traceIdFilter() {
        return (exchange, chain) -> {
            String traceId = exchange.getRequest().getHeaders().getFirst(CommonConstants.CommonRequest.TRACE_ID);
            String traceId2 = exchange.getRequest().getHeaders().getFirst(CommonConstants.CommonRequest.TRACE_ID_HEADER);
            return chain.filter(exchange)
                    .contextWrite(ctx -> ctx.put(CommonConstants.CommonRequest.TRACE_ID, traceId)); // 存入响应式上下文
        };
    }
}
